/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.meuevento.business.tests;

import com.meuevento.business.event.Event;
import com.meuevento.business.general.CrudService;
import com.meuevento.business.user.User;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Fernando Arena Varella (favarella@inf.ufrgs.br)
 */
@Stateless
public class DBPopulator {

    @EJB(name="crudService")
    CrudService<User> userService;
    @EJB(name="crudService")
    CrudService<Event> eventService;

    public DBPopulator(){
        System.out.println("DBPopulator constructor...");
    }

    public void populateDB() {
        System.out.println("Calling initDB");

        System.out.println("** Creating users **");
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUsername("user " + i);
            user.setPassword("pass " + i);
            user.setEmail("user " + i + "@meuevento.com");
            userService.create(user);
        }

        System.out.println("** Adding events to the users **");
        List<User> allUsers = userService.findWithNamedQuery(User.FIND_ALL);
        for (int i = 0; i < 10; i++) {
            //cria diversos eventos
            for (int j = 0; j < i; j++) {
                Event event = new Event();
                event.setName("Evento " + ((i*10)+j));
                event.setPlace("Local");
                event.setStartDate(new GregorianCalendar(2009, 12, 25));

                //para cada evento, adiciona alguns usuários
//                for (User user : allUsers) {
//                    event.addUser(user);
//                }
                for (int k=i; k<10; k++)
                    event.addUser(allUsers.get(k));

                eventService.create(event);
            }
        }



        System.out.println("** End of the \"populator\" **");
    }

}
